package utils.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	/**
	 * @desc 处理显示对象的工具类
	 * @author 卢业基
	 **/ 
	public class DisplayUtils
	{
		private static const COLOR_MATRIX_FILTER:ColorMatrixFilter = new ColorMatrixFilter(new Array(0.2225,0.7169,0.0606,0,0,
			0.2225,0.7169,0.0606,0,0,
			0.2225,0.7169,0.0606,0,0,
			0,0,0,1,0));
		
		//private var _blockDict:Dictionary;
		//private static var _instance:DisplayUtils=new DisplayUtils(new SingletonEnforcer());
		
		//public function DisplayUtils(enforcer:SingletonEnforcer)
		//{
		//	if(!enforcer)
		//	{
		//		throw new Error("singletonEnforcer")
		//	}			
		//}
		
		/**
		 * @author 卢业基
		 * @desc 把目标对象设为鼠标可以用或不可用
		 * @param target 要设置的对象
		 * @param value 要设置的直，true or false
		 **/ 
		public static function setMouseEnable(target:*,value:Boolean):void
		{
			if(target is MovieClip)
			{
				MovieClip(target).mouseEnabled=value;
				MovieClip(target).buttonMode=value;
				MovieClip(target).enabled=value;
			}
			if(target is Sprite)
			{
				Sprite(target).buttonMode=value;
				Sprite(target).mouseEnabled=value;
			}
			if(target is TextField)
			{
				TextField(target).mouseEnabled=value;
			}
			if(target is SimpleButton)
			{
				SimpleButton(target).enabled=value;
				SimpleButton(target).mouseEnabled=value;
				
			}
		}
		
		/**
		 * @author 卢业基
		 * @desc 把按钮设为不可用
		 **/
		public static function disabledButton(target:DisplayObject):void
		{
			if (target is Sprite)
			{
				Sprite(target).buttonMode = false;
				Sprite(target).mouseEnabled=false;
			}
			if (target is MovieClip)
			{
				MovieClip(target).enabled = false;
				MovieClip(target).buttonMode = false;
				MovieClip(target).mouseEnabled=false;
			}
			if (target is SimpleButton)
			{
				SimpleButton(target).enabled = false;
				SimpleButton(target).doubleClickEnabled=false;
				SimpleButton(target).mouseEnabled=false;
				
			}
			
			target.filters=[COLOR_MATRIX_FILTER];
		}
		
		/**
		 * @author 卢业基 
		 * @desc 把button 设为可用
		 **/ 
		public static function enableButton(target:DisplayObject):void
		{
			if (target is Sprite)
			{
				Sprite(target).buttonMode = true;
				Sprite(target).mouseEnabled=true;
			}
			else if (target is MovieClip)
			{
				MovieClip(target).enabled = true;
				MovieClip(target).buttonMode = true;
				MovieClip(target).mouseEnabled=true;
			}
			else if (target is SimpleButton)
			{
				SimpleButton(target).enabled = true;
				SimpleButton(target).mouseEnabled=true;
			}
			target.filters=[];
		}
		
		/**
		 * 深度清除资源
		 * @author abram
		 **/
		public static function clearResource(display:DisplayObject):void
		{
			if(display==null)
				return;
			if(display.parent)
				display.parent.removeChild(display);
			if(display is MovieClip||display is Sprite)
			{
				var item:DisplayObject=null;
				if(display is MovieClip)
				{
					while(MovieClip(display).numChildren>0)
					{
						item=MovieClip(display).removeChildAt(0);
						clearResource(item);	
					}
					MovieClip(display).stop();
					MovieClip(display).graphics.clear();
				}
				
				if(display is Sprite)
				{
					while(Sprite(display).numChildren>0)
					{
						item=Sprite(display).removeChildAt(0);
						clearResource(item);	
					}
					Sprite(display).graphics.clear();
				}
				
				display=null;
			}
			
			if(display is Shape)
			{
				Shape(display).graphics.clear();
				display=null;
			}
			
			if(display is Bitmap)
			{
				if(Bitmap(display).bitmapData)
					Bitmap(display).bitmapData.dispose();
				display=null;
			}
		}
		
		/**
		 * 冻结显示对象来节省性能
		 * @author abram
		 **/ 
		public static function blockDisplayObject(target:DisplayObject,stop:Boolean=false):void
		{
			var size:int=0;
			if(target is MovieClip)
			{
				MovieClip(target).mouseChildren=false;
				MovieClip(target).mouseEnabled=false;
				MovieClip(target).enabled=false;
				MovieClip(target).cacheAsBitmap=true;
				MovieClip(target).buttonMode=false;
				if(stop)
					MovieClip(target).stop();
				size=MovieClip(target).numChildren;
			}
			
			if(target is Sprite)
			{
				Sprite(target).mouseChildren=false;
				Sprite(target).mouseEnabled=false;
				Sprite(target).cacheAsBitmap=true;
				Sprite(target).buttonMode=false;
				size=Sprite(target).numChildren;
			}
			if(target is MovieClip||target is Sprite)
			{
				for(var i:int=0;i<size;i++)
				{
					if(target is MovieClip)
						blockDisplayObject(MovieClip(target).getChildAt(i),stop);
					else
						blockDisplayObject(Sprite(target).getChildAt(i),stop);
				}
			}
		}
		
		/**
		 * 把mc画成位图数组
		 * @author abram
		 * @param mc 需要转换的mc
		 * @return array
		 **/ 
		public static function drawMovieClip(mc:MovieClip,callback:Function):void
		{
			if(mc==null) return;
			var frames:int=mc.totalFrames;
			var bitmaps:Array=[];
			var rect:Rectangle=mc.getBounds(mc);
			var readFrame:int=1;
			mc.addEventListener(Event.ENTER_FRAME,dragHandler);	
			mc.gotoAndPlay(1);
			
			function dragHandler(evt:Event):void
			{
				if(mc.currentFrame>=readFrame)
				{
					var bitmapData:BitmapData = new BitmapData(Math.ceil(rect.width),Math.ceil(rect.height),true,0);
					var m:Matrix;
					if (rect)
					{
						m = new Matrix();
						m.translate(-rect.x,-rect.y);
					}
					bitmapData.draw(mc,m);
					bitmaps.push(bitmapData);
					if(mc.currentFrame>=mc.totalFrames)
					{
						mc.stop();
						mc.removeEventListener(Event.ENTER_FRAME,dragHandler);
						callback(bitmaps);
					}
					readFrame++;
				}
			}
			
		}
	}
}

/**
 * @author 单例限制类，防止外部常见DataCache实例
 */ 
class SingletonEnforcer{}